package cn.edu.ujn.booksshop.controller;

import java.sql.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.ujn.booksshop.dao.Order;
import cn.edu.ujn.booksshop.service.OrderService;
import cn.edu.ujn.util.Page;

@Controller
public class OrderController {

	@Autowired
	private OrderService ord;
	@GetMapping("order/list.action")
	public String list(Model model,@RequestParam (defaultValue = "1")  Integer page ,@RequestParam(defaultValue ="10")  Integer rows) {
		Page<Order> selectAll = this.ord.selectAll(page, rows);
		model.addAttribute("page", selectAll);
		return "order";
	}
	@PostMapping("order/add.action")
	public String add(@RequestParam (defaultValue = "0")  Integer orderId,@RequestParam(defaultValue ="0")  Integer userId,@RequestParam(defaultValue ="0")  Date Date) {
		Order order = new Order();
		order.setOrderid(orderId);
		order.setUserid(userId);
		order.setOrderdate(Date);
		int insert = this.ord.add(order);
		return "order";
	}
	@PostMapping("order/delete.action")
	public String delete(Integer id) {
		this.ord.delete(id);
		return "order";
//		return "order";
	}
	
	@PostMapping("order/update.action")
	public String update(@RequestParam (defaultValue = "0")  Integer orderId,@RequestParam(defaultValue ="0")  Integer edituserId,@RequestParam(defaultValue ="10")  Date editDate) {
		Order order = new Order();
		order.setOrderid(orderId);
		order.setUserid(edituserId);
		order.setOrderdate(editDate);
		int updateByPrimaryKeySelective = this.ord.updateByPrimaryKeySelective(order);
		return "order";
	}
}
